package org.ontobrowser.ontoextract;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;


public class Main {
	public static void main(String[] args) {
		try {
			//URL url = new URL("http://xmlns.com/foaf/spec/index.rdf");
			URL url = new URL("http://www.w3.org/2000/01/rdf-schema");
			
			//http://www.w3.org/2000/01/rdf-schema
			
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		    dbf.setValidating(false);
		    dbf.setNamespaceAware(true);
		    dbf.setIgnoringElementContentWhitespace(true);

			// Parse the input to produce a parse tree with its root
			// in the form of a Document object
			Document doc = null;
			    
		    DocumentBuilder builder = dbf.newDocumentBuilder();
		    //builder.setErrorHandler(new MyErrorHandler());
		    //InputSource is = new InputSource(filename);
		      
		    doc = builder.parse(url.openStream());
		    
		    getClazzNode(url.toString());
		    //print(doc.getDocumentElement());
		    
		    
			
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	

}
	
	private static List<Class> getClazzNode(String urls) throws SAXException, IOException, ParserConfigurationException
	{
		URL url = new URL(urls);
		
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
	    dbf.setValidating(false);
	    dbf.setNamespaceAware(true);
	    dbf.setIgnoringElementContentWhitespace(true);

		// Parse the input to produce a parse tree with its root
		// in the form of a Document object
		Document doc = null;
		    
	    DocumentBuilder builder = dbf.newDocumentBuilder();
	    doc = builder.parse(url.openStream());
	    
	    Element element = doc.getDocumentElement();
	    
	    
	    
	    List<Class> clazzs = RDFConversionUtil.extractClass(element);
	    
	    
	    return clazzs;
	}
	
	private static Node getClazzProperties(String urls) throws SAXException, IOException, ParserConfigurationException
	{
		URL url = new URL(urls);
		
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
	    dbf.setValidating(false);
	    dbf.setNamespaceAware(true);
	    dbf.setIgnoringElementContentWhitespace(true);

		// Parse the input to produce a parse tree with its root
		// in the form of a Document object
		Document doc = null;
		    
	    DocumentBuilder builder = dbf.newDocumentBuilder();
	    doc = builder.parse(url.openStream());
	    
	    Element element = doc.getDocumentElement();
	    
	    NodeList nodeList = element.getElementsByTagName("rdfs:Class");
	    nodeList.getLength();
	    
	    Node n = null;
	    return n;
	}
	
	private static void print(Node node) throws DOMException, SAXException, IOException, ParserConfigurationException
	{
		System.out.println(node.getNodeName());
		
		 if(node.getAttributes() != null)
		    {
		    	int lenght = node.getAttributes().getLength();
			    
			    for(int i=0; i<lenght; i++)
			    {
			    	System.out.println(node.getAttributes().item(i).toString());
			    }			    
		    }
		    
		    NodeList nodeList = node.getChildNodes();
		    
		    if(nodeList != null)
		    {
		    	int lenght = nodeList.getLength();
			    
			    for(int i=0; i<lenght; i++)
			    {
			    	node = nodeList.item(i);			    	
			    	if(node.getNodeType() == Node.ELEMENT_NODE)
			    	{			    		
			    		if(node.getLocalName().equals("Property"))
			    		{
				    		System.out.println(node.getLocalName());
				    		System.out.println(node.getPrefix());
				    		System.out.println(node.getNamespaceURI());
				    		
				    		 if(node.getAttributes() != null)
				 		    {
				 		    	int lenghtatt = node.getAttributes().getLength();
				 			    
				 			    for( int j=0; j<lenghtatt; j++)
				 			    {
				 			    	System.out.println(node.getAttributes().item(j).toString());
				 			    }			    
				 		    }
				    		 
				    		 NodeList childNodeList = node.getChildNodes();
					    	 if(childNodeList != null)
							 {
							    	int lenghtCHILD = childNodeList.getLength();
								    
								    for(int J=0; J<lenghtCHILD; J++)
								    {
								    	node = childNodeList.item(J);
								    	if(node.getNodeType() == Node.ELEMENT_NODE)
								    	{
								    		if(node.getLocalName().equals("type"))
								    		{
									    		System.out.println(node.getLocalName());
									    		System.out.println(node.getPrefix());
									    		System.out.println(node.getNamespaceURI());
									    		
									    		 if(node.getAttributes() != null)
									 		    {
									 		    	int lenghtChild = node.getAttributes().getLength();
									 			    
									 			    for(int l=0; l<lenghtChild; l++)
									 			    {
									 			    	node = node.getAttributes().item(l);
									 			    	String localName = node.getLocalName();
									 			    	if(localName.equals("resource"))
									 			    	{
									 			    		System.out.println(node.getNodeValue().toString());
									 			    		Node nodeClazz = getClazzProperties(node.getNodeValue());
									 			    		
									 			    		print(nodeClazz);
									 			    	}
									 			    }			    
									 		    }
								    		}
								    	}
								    }			    
							    }

			    		}
			    	}
			    	 
			   }			    
		    }
		    
		   // doc.getElementsByTagNameNS(namespaceURI, localName);
			
		
	}
}


